install.packages("palmerpenguins")
library(palmerpenguins)
palmerpenguins::penguins   
       #示例数据来源于palmerpenguins包，由Kristen Gorman博士和南极洲帕尔默站 LTER penguins收集和提供的344个观测值构成。

setwd("E://OneDrive//1-教学//科技论文写作//课件//课上使用的文档//2.9.5-科研绘图//散点图")

library(ggplot2)

ggplot(penguins)


ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g))


ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species))


ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species))+
  geom_smooth(method = "lm", aes(x = flipper_length_mm, y = body_mass_g))


lm <- lm(body_mass_g ~ flipper_length_mm, data = penguins) #直线回顾函数的斜率和截距
summary(lm)  #R^2 = 0.76 p < 0.001

ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species)) +
  geom_smooth(method = "lm", aes(x = flipper_length_mm, y = body_mass_g)) +
  annotate("text", x = 180, y = 6000, label = "paste(italic(R) ^ 2, \" = 0.76 \", italic(p), \" < 0.001\")", 
           parse = T,  color = "black", size=4)


ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species)) +
  geom_smooth(method = "lm", aes(x = flipper_length_mm, y = body_mass_g)) +
  annotate("text", x = 180, y = 6000, label = "paste(italic(R) ^ 2, \" = 0.76 \", italic(p), \" < 0.001\")", 
           parse = T,  color = "black", size=4)+
  labs(
    title = "Body mass and flipper length",                             #标题
    subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", #副标题
    x = "Flipper length (mm)", y = "Body mass (g)",                     #坐标轴名
    color = "Species", shape = "Species"                                #图例
  )


library(RColorBrewer)

ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species)) +
  geom_smooth(method = "lm", aes(x = flipper_length_mm, y = body_mass_g)) +
  annotate("text", x = 180, y = 6000, label = "paste(italic(R) ^ 2, \" = 0.76 \", italic(p), \" < 0.001\")", 
           parse = T,  color = "black", size=4)+
  labs(
    title = "Body mass and flipper length",                             #标题
    subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", #副标题
    x = "Flipper length (mm)", y = "Body mass (g)",                     #坐标轴名
    color = "Species", shape = "Species"                                #图例
  ) +
  scale_color_manual(values = brewer.pal(8,"Set1"))


ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species)) +
  geom_smooth(method = "lm", aes(x = flipper_length_mm, y = body_mass_g)) +
  annotate("text", x = 180, y = 6000, label = "paste(italic(R) ^ 2, \" = 0.76 \", italic(p), \" < 0.001\")", 
           parse = T,  color = "black", size=4)+
  labs(
    title = "Body mass and flipper length",                             #标题
    subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", #副标题
    x = "Flipper length (mm)", y = "Body mass (g)",                     #坐标轴名
    color = "Species", shape = "Species"                                #图例
  ) +
  scale_color_manual(values = brewer.pal(8,"Set1"))+
  theme_bw()


Fig1 <- ggplot(penguins)+
  geom_point(aes(x = flipper_length_mm, y = body_mass_g, color = species, shape = species)) +
  geom_smooth(method = "lm", aes(x = flipper_length_mm, y = body_mass_g)) +
  annotate("text", x = 180, y = 6000, label = "paste(italic(R) ^ 2, \" = 0.76 \", italic(p), \" < 0.001\")", 
           parse = T,  color = "black", size=4)+
  labs(
    title = "Body mass and flipper length",                             #标题
    subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins", #副标题
    x = "Flipper length (mm)", y = "Body mass (g)",                     #坐标轴名
    color = "Species", shape = "Species"                                #图例
  ) +
  scale_color_manual(values = brewer.pal(8,"Set1"))+
  theme_bw()

ggsave(filename = "Fig1.png", plot = Fig1, width = 7, height = 4, bg = "white")
ggsave(filename = "Fig1.svg", plot = Fig1, width = 7, height = 4, bg = "white")
#修改文件名的后缀确定图片格式